<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Linux挂载swap分区</title>
    <style type="text/css" media="all">
      body {
        margin: 0;
        font-family: "Helvetica Neue", Helvetica, Arial, "Hiragino Sans GB", sans-serif;
        font-size: 14px;
        line-height: 20px;
        color: #777;
        background-color: white;
      }
      .container {
        width: 700px;
        margin-right: auto;
        margin-left: auto;
      }

      .post {
        font-family: Georgia, "Times New Roman", Times, "SimSun", serif;
        position: relative;
        padding: 70px;
        bottom: 0;
        overflow-y: auto;
        font-size: 16px;
        font-weight: normal;
        line-height: 25px;
        color: #515151;
      }

      .post h1{
        font-size: 50px;
        font-weight: 500;
        line-height: 60px;
        margin-bottom: 40px;
        color: inherit;
      }

      .post p {
        margin: 0 0 35px 0;
      }

      .post img {
        border: 1px solid #D9D9D9;
      }

      .post a {
        color: #28A1C5;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="post">
        <h1 class="title">Linux挂载swap分区</h1>
        <div class="show-content">
          <p>1.内存占用情况<br></p><p>alic@alic-ThinkPad-X201:~$ free</p><p>total used free shared buffers cached</p><p>Mem: 3939884 3662588 277296 230612 208836 2674672</p><p>-/+ buffers/cache: 779080 3160804</p><p>Swap: 0 0 0</p><p>2.通过dd命令增加swap</p><p>alic@alic-ThinkPad-X201:~$ dd if=/dev/zero of=/var/swap bs=1024 count=2048000</p><p>2048000+0 records in</p><p>2048000+0 records out</p><p>2097152000 bytes (2.1 GB) copied, 5.9442 s, 353 MB/s</p><p>3.创建swap文件</p><p>alic@alic-ThinkPad-X201:~$ mkswap /var/swap</p><p>mkswap: /var/swap: warning: don't erase bootbits sectors</p><p>on whole disk. Use -f to force.</p><p>Setting up swapspace version 1, size = 2047996 KiB</p><p>no label, UUID=daa18338-0371-4848-888d-49d662dafe3f</p><p>[root@mysql01 var]# mkswap -f /var/swap</p><p>Setting up swapspace version 1, size = 2047996 KiB</p><p>no label, UUID=754e3fdf-258c-45f2-bb8e-4725862850d3</p><p>4.加载该文件：</p><p>alic@alic-ThinkPad-X201:~$ swapon /var/swap</p><p>5.看看swap是否生效</p><p>[root@mysql01 var]# free</p><p>6.查看swap当前状态：</p><p>[root@mysql01 var]# cat /proc/swaps</p><p>Filename Type Size Used Priority</p><p>/var/swap file 2047992 0 -1</p><p>7.添加至/etc/fstab</p><p>alic@alic-ThinkPad-X201:~$ vim /etc/fstab</p><p>#</p><p># /etc/fstab</p><p># Created by anaconda on Tue Sep 23 17:58:53 2014</p><p>#</p><p># Accessible filesystems, by reference, are maintained under '/dev/disk'</p><p># See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info</p><p>#</p><p>UUID=98f4128a-2ac6-4822-8624-300c4d818afc / ext4 defaults 1 1</p><p>tmpfs /dev/shm tmpfs defaults 0 0</p><p>devpts /dev/pts devpts gid=5,mode=620 0 0</p><p>sysfs /sys sysfs defaults 0 0</p><p>proc /proc proc defaults 0 0</p><p>/var/swap swap swap defaults 0 0</p><p>8.如果不再需要swap，可以清理该分区：</p><p>alic@alic-ThinkPad-X201:~$ swapoff /var/swap</p>
        </div>
      </div>
    </div>
  </body>
</html>
